package com.example.demo04;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

@Slf4j
@MapperScan(basePackageClasses = {TestMapper.class})
@SpringBootApplication
public class Demo04Application {

    @Autowired
    private TestMapper testMapper;

    public static void main(String[] args) {
        SpringApplication.run(Demo04Application.class, args);

    }

    @PostConstruct
    public void testMethod() {
        Integer tableCount = testMapper.selectTable();
        System.out.println(tableCount + "---------------------");
        log.info("---------------------:{}",tableCount);
        if (Objects.isNull(tableCount) || tableCount <= 0) {
            testMapper.createTestTable();
        }
        TestEntity testEntity=new TestEntity();
        testEntity.setId(IdUtil.fastUUID());
        testEntity.setName("abc"+RandomUtil.randomString("abcdefghijkmnopqrstuvwxyz",16));
        int insert = testMapper.insert(testEntity);
        log.info("插入数据:{}",insert);
        CompletableFuture.supplyAsync(()->testMapper.selectCount(new LambdaQueryWrapper<TestEntity>().eq(TestEntity::getName, "%abc")))
                .thenAccept(result->{
                    log.info("查询数据 count:{}",result);
                }).join();
    }

}
